package com.haier.bims.samples.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.haier.bims.samples.bean.DropDown;
import com.haier.bims.samples.bean.Samples;
import com.haier.bims.samples.service.ISampleService;
import com.haier.common.AbstractController;

@Controller
public class SampleController extends AbstractController{
	
	private static Logger logger = Logger.getLogger(SampleController.class);

	private ISampleService sampleService;

	public void setSampleService(ISampleService sampleService) {
		this.sampleService = sampleService;
	}

	/**
	 * 获得某个样本信息(根据样本Id集合获取json数据)
	 * 
	 * @return 样本信息
	 */
	@RequestMapping(method=RequestMethod.GET, value="/sample/{ids}")
	@ResponseBody
	public String getEachSamkleInfo(@PathVariable String ids,@RequestParam("callback") String callback,@RequestHeader("Accept")String accept) {
		
		List<Samples> list = sampleService.getSampleInfoByids(ids);
		String result = this.pojo2String(list, accept);
		result = callback+result;
		return result;
	}
	
	
	/**
	 * 创建样本信息(新建样本)
	 * 
	 * @return 样本ID
	 */
	@RequestMapping(method=RequestMethod.GET, value="/sample/register")
	@ResponseBody
	public String insertEachSample(
			@RequestParam("x") String x,
			@RequestParam("y") String y,
			@RequestParam("typeId") String typeId,
			@RequestParam("unitValue") String unitValue,
			@RequestParam("unitTypeId") String unitTypeId,
			@RequestParam("parentId") String parentId,
			@RequestParam("projectId") String projectId,
			@RequestParam("sectionId") String sectionId,
			@RequestParam("patientId") String patientId,
			@RequestParam("project") String projectName,
			@RequestParam("section") String sectionName,
			@RequestParam("patient") String patient,
			@RequestParam("type") String type,
			@RequestParam("unitType") String unitType,
			@RequestParam("callback") String callback,
			@RequestHeader("Accept")String accept) {
		String param = "x="+x+"&y="+y+"&typeId="+typeId+"&unitValue="+unitValue+"&unitTypeId="+unitTypeId+"&parentId="+parentId+"&projectId="+projectId+"&sectionId="+sectionId+"&patientId="+patientId+"&project="+projectName+"&section="+sectionName+"&patient="+patient+"&type="+type+"&unitType="+unitType;
		String samid = sampleService.insertEachSample(param);
		String result =callback+"([{\"sampleId\":\""+samid+"\"}])";
		return result;
	}
	
	/**
	 * 更新样本信息(根据样本Id更新样本信息)
	 * 
	 * @return success
	 */
	@RequestMapping(method=RequestMethod.GET, value="/sample/update")
	@ResponseBody
	public String updateEachSample(@RequestParam("x") String x,
			@RequestParam("y") String y,
			@RequestParam("parentId") String parentId,
			@RequestParam("sampleId") String sampleId,
			@RequestParam("callback") String callback,
			@RequestHeader("Accept")String accept) {
		
		String param = "x="+x+"&y="+y+"&parentId="+parentId+"&sampleId="+sampleId;
		String successFlag = sampleService.updateEachSample(param);
		String result =callback+"([{\"success\":\""+successFlag+"\"}])";
		return result;
	}
	
	
	/**
	 * 获取样本模糊查询数据(模糊搜索)
	 * 
	 * @return success
	 */
	@RequestMapping(method=RequestMethod.GET, value="/sample/dropdownBytypes")
	@ResponseBody
	public String getDropDownBytypes(@RequestParam("q") String q,@RequestParam("callback") String callback,@RequestHeader("Accept")String accept) {
		
		
		try {
			q = new String(q.getBytes("iso-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		List<DropDown> list = sampleService.getDropDownListByTypes(q);
		String result = this.pojo2String(list, accept);
		result = callback+result;
		return result;
	}
	/**
	 * 获取样本模糊查询数据(模糊搜索)
	 * 
	 * @return success
	 */
	@RequestMapping(method=RequestMethod.GET, value="/sample/dropdownByPatients")
	@ResponseBody
	public String getDropDownByPatients(@RequestParam("q") String q,@RequestParam("callback") String callback,@RequestHeader("Accept")String accept) {
		
		try {
			q = new String(q.getBytes("iso-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		List<DropDown> list = sampleService.getDropDownListByPatients(q);
		String result = this.pojo2String(list, accept);
		result = callback+result;
		return result;
	}
	/**
	 * 获取样本模糊查询数据(模糊搜索)
	 * 
	 * @return success
	 */
	@RequestMapping(method=RequestMethod.GET, value="/sample/dropdownByProjects")
	@ResponseBody
	public String getDropDownByProjects(@RequestParam("q") String q,@RequestParam("callback") String callback,@RequestHeader("Accept")String accept) {
		
		try {
			q = new String(q.getBytes("iso-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		List<DropDown> list = sampleService.getDropDownListByProjects(q);
		String result = this.pojo2String(list, accept);
		result = callback+result;
		return result;
	}
	/**
	 * 获取样本模糊查询数据(模糊搜索)
	 * 
	 * @return success
	 */
	@RequestMapping(method=RequestMethod.GET, value="/sample/dropdownByUnits")
	@ResponseBody
	public String getDropDownByUnits(@RequestParam("callback") String callback,@RequestHeader("Accept")String accept) {
		
		List<DropDown> list = sampleService.getDropDownListByUnits();
		String result = this.pojo2String(list, accept);
		result = callback+result;
		return result;
	}
	
	
	/**
	 * 获取扫描数据
	 * 
	 * @return success
	 */
	@RequestMapping(method=RequestMethod.GET, value="/sample/scanBycode")
	@ResponseBody
	public String getObjectInfoByCode(@RequestParam("code") String code,@RequestParam("callback") String callback,@RequestHeader("Accept")String accept) {
		
		Map list = sampleService.getObjectInfoByCode(code);
		String result = this.pojo2String(list, accept);
		result = callback+result;
		return result;
	}
	
}
